package cn.briarbear.myblog.service.impl;

import cn.briarbear.myblog.entity.dto.BlogAddForm;
import cn.briarbear.myblog.entity.dto.IdListForm;
import cn.briarbear.myblog.entity.table.Blog;
import cn.briarbear.myblog.entity.table.Tag;
import cn.briarbear.myblog.entity.table.TagBlog;
import cn.briarbear.myblog.entity.vo.BlogFormView;
import cn.briarbear.myblog.entity.vo.BlogModifyView;
import cn.briarbear.myblog.entity.vo.BlogView;
import cn.briarbear.myblog.service.IBlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.briarbear.myblog.repository.BlogRepository;
import cn.briarbear.myblog.repository.TagBlogRepository;
import cn.briarbear.myblog.repository.TagResository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
//@CacheConfig(cacheNames = "blog")
@Service
public class BlogServiceImpl implements IBlogService {

    @Autowired
    private BlogRepository blogRepository;
    @Autowired
    private TagResository tagResository;
    @Autowired
    private TagBlogRepository tagBlogRepository;

//    @Autowired
//    private CacheManager cacheManager;

    @Override
    public List<BlogView> getAllBlogView() {
        List<BlogView> viewList = new ArrayList<>();
        List<Blog> blogList =  blogRepository.findAll();

        for (Blog blog : blogList) {
            viewList.add(new BlogView(blog));
        }
        return viewList;
    }

    @Override
    public List<BlogFormView> getAdminBlogFormList() {
        List<BlogFormView> viewList = new ArrayList<>();
        List<Blog> blogs = blogRepository.findAll();
        for (Blog blog : blogs) {
            //由tiamstamp转Date
//            blog.setCreateTime(new Date(blog.getCreateTime().getTime()));
//            blog.setModifyTime(new Date(blog.getModifyTime().getTime()));
            viewList.add(new BlogFormView(blog));
        }
        return viewList;
    }


    @Override
    public Blog getBlogById(Integer id) {
        return blogRepository.findById(id).get();
    }

    @Override
    public void blogAdd(BlogAddForm form) {
        Blog blog = new Blog("new");
        blog.setTitle(form.getTitle());
        blog.setMdContent(form.getMdContent());
        blog.setHtmlContent(form.getHtmlContent());
        blog.setIntroduction(form.getIntroduction());
        //持久化
        blogRepository.save(blog);


        //得到标签
        String[] tags = form.getRawTags().split(",");

        for (String tag : tags) {
            Tag tag1 = tagResository.findByName(tag);
            if (tag1!=null){  //如果标签存在，
                Integer id = tag1.getId();
                TagBlog tagBlog = new TagBlog();
                tagBlog.setBlogId(blog.getId());
                tagBlog.setTagId(id);

                tagBlogRepository.save(tagBlog);

            }else {
                Tag tag2 = new Tag();
                tag2.setCreateTime(new Date());
                tag2.setName(tag);
                tagResository.save(tag2);

                Integer tagId = tag2.getId();
                TagBlog tagBlog = new TagBlog();
                tagBlog.setTagId(tagId);
                tagBlog.setBlogId(blog.getId());
                tagBlogRepository.save(tagBlog);

            }
        }



    }



    @Override
    public List<BlogView> searchBlogByKeyword(String keyword) {
        List<BlogView> viewList = new ArrayList<>();
        List<Blog> blogList =  blogRepository.findByTitleContainingOrIntroductionContaining(keyword,keyword);

        for (Blog blog : blogList) {
            viewList.add(new BlogView(blog));
        }
        return viewList;
    }


    @Override
    public List<BlogView> getBlogByTagId(Integer tagId) {
        List<BlogView> viewList = new ArrayList<>();
        List<Blog> blogList = blogRepository.findByTagId(tagId);

        for (Blog blog : blogList) {
            viewList.add(new BlogView(blog));
        }
        return viewList;
    }


    @Override
    public void blogModify(BlogModifyView blogModifyView) {
        Blog blog = new Blog(blogModifyView);

        blogRepository.save(blog);
    }

    @Override
    public void blogDelete(IdListForm IdListForm) {
        for (Integer id : IdListForm.getIds()) {

            if (blogRepository.findById(id).isPresent())
                blogRepository.deleteById(id);
            if (tagBlogRepository.findById(id).isPresent())
                tagBlogRepository.deleteByBlogId(id);
        }
    }
}
